// pages/order/order.js
// pages/record/record.js
const app = getApp()
const DB = wx.cloud.database().collection("list")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    startDate: null,
    endDate: null,
    index: 1,
    order: {
      date: null,
      name: null,
      cardID: null,
      mobile: null,
      campus: '航空港校区'
    },
    modalName: null,
    cardIDValid: true,
    nameValid: true,
    mobileValid: true,
    msg: '',
    index: 0,
    picker: ['航空港校区', '龙泉驿校区'],
  },
  //============================
  //姓名改变
  nameChange(e) {
    this.setData({
      ['order.name']: e.detail.value
    })
  },
  // 身份证改变
  cardIDchange(e) {
    this.setData({
      ['order.cardID']: e.detail.value
    })
  },
  //电话号码改变
  mobilechange(e) {
    this.setData({
      ['order.mobile']: e.detail.value
    })
  },
  //校区选择改变
  PickerChange(e) {
    this.setData({
      index: e.detail.value,
      ['order.campus']: this.data.picker[e.detail.value]
    })
  },
  // 时间选择改变
  addDateChange(e) {
    this.setData({
      ['order.date']: e.detail.value
    })
    const time = '2020-04-28'
    const chooseDate = new Date(e.detail.value)
    const nowDate = new Date(time)
    // console.log((chooseDate - nowDate) / 24 / 3600 / 1000 )
  },
  //名字验证
  validateName(e) {
    var ts = this;
    var name = e.detail.value
    if (e.detail.value == '') {
      return wx.showToast({
        title: '姓名不能为空',
        icon: 'none'
      })
    }
    var reg = /^[\u4E00-\u9FA5\uf900-\ufa2d·s]{2,6}$/;

    if (!name.match(reg)) { 
      wx.showToast({
        title: '请正确输入姓名',
        icon: 'none'
      })
      this.setData({
        nameValid: false
      })
    } else {
      this.setData({
        nameValid: true
      })
    }
  },
  //验证身份证
  validateCardID(e) {
    var ts = this;
    var code = e.detail.value
    if(code == '') {
      return wx.showToast({
        title: '身份证号不能为空',
        icon: 'none'
      })
    }
    //身份证号合法性验证 
    //支持15位和18位身份证号
    //支持地址编码、出生日期、校验位验证
    var city = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江 ", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北 ", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏 ", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外 " };
    var tip = "";
    var pass = true;
    var reg = /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/;
    if (!code || !code.match(reg)) {
      wx.showToast({
        title: '身份证号格式错误',
        icon: 'none'
      })
      this.setData({
        cardIDValid: false
      })
    } else if (!city[code.substr(0, 2)]) {
      wx.showToast({
        title: '地址编码错误',
        icon: 'none'
      })
      this.setData({
        cardIDValid: false
      })
    } else {
      //18位身份证需要验证最后一位校验位
      if (code.length == 18) {
        code = code.split('');
        //∑(ai×Wi)(mod 11)
        //加权因子
        var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
        //校验位
        var parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
        var sum = 0;
        var ai = 0;
        var wi = 0;
        for (var i = 0; i < 17; i++) {
          ai = code[i];
          wi = factor[i];
          sum += ai * wi;
        }
        var last = parity[sum % 11];
        if (parity[sum % 11] != code[17]) {
          wx.showToast({
            title: '校验位错误',
            icon: 'none'
          })
          this.setData({
            cardIDValid: false
          })
        } else {
          this.setData({
            cardIDValid: true
          })
        }
      }
    }
  },
  //验证手机号
  validateMobile(e) {
    var myreg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
    if (!myreg.test(e.detail.value)) {
      wx.showToast({
        title: '请正确输入手机号',
        icon: 'none'
      })
      this.setData({
        mobileValid: false
      })
    } else {
      this.setData({
        mobileValid: true
      })
    }
  },
  //添加数据
  order() {
    if (this.data.order.mobile == null || this.data.order.mobile == '' || this.data.order.name == null || this.data.order.name == '' || this.data.order.cardID == null || this.data.order.cardID == '') {
      return wx.showToast({
        title: '请将信息填写完整',
        icon: 'none'
      })
    }else if(this.data.nameValid == false) {
      wx.showToast({
        title: '请正确输入姓名',
        icon: 'none'
      })
    } else if (this.data.cardIDValid == false) {
      wx.showToast({
        title: '身份证格式错误',
        icon: 'none'
      })
    } else if (this.data.mobileValid == false) {
      wx.showToast({
        title: '请正确输入手机号',
        icon: 'none'
      })
    } else {
      wx.showLoading({})
      wx.cloud.callFunction({
        name: 'order',
        data: this.data.order
      }).then(res => {
        // if (res.result.status != 200) {
        //   // return wx.showToast({
        //   //   title: res.result.msg,
        //   //   icon: 'none'
        //   // })
        //   return this.setData({
        //     msg: res.result.msg,
        //     modalName: 'Modal'
        //   })
        // } else {
        //   this.setData({
        //     msg: res.result.msg,
        //     modalName: 'Modal'
        //   })
        // }
        this.setData({
          msg: res.result.msg,
          modalName: 'Modal'
        })
        wx.hideLoading()
        // 跳转到我的预约
        // wx.redirectTo({
        //   url: '/pages/myOrder/myOrder'
        // })
      })
    }
  },
  //获取当前年月
  getTime() {
    let timestamp = Date.parse(new Date())
    let nowDate = new Date(timestamp)
    let year = nowDate.getFullYear();
    let month = (nowDate.getMonth() + 1 < 10 ? '0' + (nowDate.getMonth() + 1) : nowDate.getMonth() + 1);
    let day = (nowDate.getDate() < 10 ? '0' + nowDate.getDate() : nowDate.getDate());
    let date = year + '-' + month + '-' + day
    this.setData({
      ['order.date']: date,
      startDate: date
    })
    //获取七天后的日期
    let now = new Date()
    nowDate = new Date(now.getTime() + 7 * 24 * 3600 * 1000)
    year = nowDate.getFullYear();
    month = (nowDate.getMonth() + 1 < 10 ? '0' + (nowDate.getMonth() + 1) : nowDate.getMonth() + 1);
    day = (nowDate.getDate() < 10 ? '0' + nowDate.getDate() : nowDate.getDate());
    date = year + '-' + month + '-' + day
    this.setData({
      endDate: date
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  onLoad: function (options) {
    this.getTime()
  }
})